#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

#import "FWApplication.h"
#import "FWListViewDelegate.h"
#import "FWApplicationDelegate.h"
#import "FWPreferences.h"

#define localizeGlobal(str) [self.plugin.globalBundle localizedStringForKey:str value:str table:nil]


extern NSInteger const kStatusLineIndex;
extern NSInteger const kToolbarIndex;


@class LIPlugin;
@class SBApplication;
@class FWStatusbarViewCell;


@protocol FWPluginOptional <NSObject>

@optional

// Statusbar support
- (NSArray*)statusbarItems;
- (BOOL)configureStatusbarItems:(NSArray*)items;
- (BOOL)isStatusbarRequired;
- (NSString*)statusbarCaption;
- (NSString*)statusbarBadge;
- (NSString*)statusbarCaptionIcon;
- (NSInteger)statusbarIndent;
- (BOOL)shouldRebuildStatusbar:(NSArray*)items;

// Toolbar support
- (NSArray*)toolbarItems;
- (BOOL)configureToolbarItems:(NSArray*)items;


- (void)repeatingEventNotification:(NSNotification*)notification;
- (void)scheduledEventNotification:(NSDate*)scheduledDate;

@end


@interface FWPlugin : NSObject <LIPluginController, LITableViewDelegate, UITableViewDataSource, FWPluginOptional> 
{
    id<FWListViewDelegate> fwDatasource;
    FWApplication* fwApplication;
    NSDate* lastUpdate;
    FWPreferences* preferences;

//    FWStatusbarViewCell* statusbarView;
    
    NSString* resourceName;
    NSString* notificationName;
    
//    NSDate* alarmDate;
//    NSDate* muteUntilDate;
//    NSURL *alarmSound;
//    CFURLRef alarmSoundURLRef;
//    BOOL alarmVibrate;
//	SystemSoundID alarmSoundObject;
    volatile BOOL listLock;
    NSObject* mutexLock;
    
    NSDate* nextScheduledActivity;
    
    NSInteger toolbarRow;
    NSDate* toolbarOpenedDate;
}


@property (retain) LIPlugin* liPlugin;
@property (nonatomic, retain) NSDate* lastUpdate;
@property (retain) NSArray* items;
@property (retain) FWApplication* fwApplication;
@property (retain) id<FWListViewDelegate> fwDatasource;
@property (nonatomic, retain) FWPreferences* preferences;
@property BOOL showStatusbar;
@property (retain) FWStatusbarViewCell* statusbarView;
@property (retain) NSString* notificationName;
@property (retain) NSTimer* timer;
@property (retain) NSString* resourceName;
@property (readonly) NSDate* nextScheduledActivity;


- (id)initWithApplication:(FWApplication*)app;
- (id)initWithApplication:(FWApplication*)app name:(NSString*)name;
- (void)dealloc;

- (void)updateList;
- (void)repeatingNotification:(NSNotification*)notif;
- (void)updateListForNotification;

- (NSInteger)indexForRowAtIndexPath:(NSIndexPath *)indexPath withToolbar:(BOOL)toolbar;

- (void)timerNotification:(NSNotification*)notif;
- (void)viewReadyNotification:(NSNotification*)notif;
- (void)screenUndimmedNotification:(NSNotification*)notif;
- (void)windowVisibleNotification:(NSNotification*)notif;

- (void)triggerRedisplay;
- (void)triggerRedisplayForTableView:(UITableView*)tableView;
- (void)triggerUnconditionalRefresh;
- (void)timerTriggered:(NSTimer*)timer;

- (void)setBadgeValue:(NSString*)value hidden:(BOOL)hidden;

- (void)scheduleNextTimerEvent:(NSDate*)date;

- (BOOL)tableViewIsToolbarVisible:(UITableView *)tableView;
- (void)tableView:(UITableView *)tableView setToolbarIndex:(NSInteger)index withAnimation:(BOOL)animate;
- (void)tableView:(UITableView *)tableView setToolbarIndex:(NSInteger)index withAnimation:(BOOL)animate updatingRow:(NSInteger)row;
- (void)tableView:(UITableView *)tableView hideToolbarWithAnimation:(BOOL)animate;
- (void)setToolbarIndex:(NSInteger)index;
- (NSInteger)toolbarIndex;

- (CGRect)previewCompatibleRect;
- (NSDictionary*)itemForTableRow:(NSInteger)row;

@end
